package com.android.whatsblue.util;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.graphics.drawable.Drawable;
import android.util.Log;

public class BluetoothUtils {
	
	class PInfo
	{
	    private String appname = "";
	    private String pname = "";
	    private String versionName = "";
	    private int versionCode = 0;
	    private Drawable icon;
	    private String printObject() {
	        return appname + "\t" + pname + "\t" + versionName + "\t" + versionCode;
	    }

	}
	
	public ArrayList<PInfo> getPackages(Context content) {
	    ArrayList<PInfo> apps = getInstalledApps(content, false); /* false = no system packages */
	    final int max = apps.size();
	    for (int i=0; i<max; i++) {
	    	Log.d("APPS", apps.get(i).printObject());
	    }
	    return apps;
	}
	
	private ArrayList<PInfo> getInstalledApps(Context content, boolean getSysPackages) {
	    ArrayList<PInfo> res = new ArrayList<PInfo>();        
	    List<PackageInfo> packs = content.getPackageManager().getInstalledPackages(0);
	    for(int i=0;i<packs.size();i++) {
	        PackageInfo p = packs.get(i);
	        if ((!getSysPackages) && (p.versionName == null)) {
	            continue ;
	        }
	        PInfo newInfo = new PInfo();
	        newInfo.appname = p.applicationInfo.loadLabel(content.getPackageManager()).toString();
	        newInfo.pname = p.packageName;
	        newInfo.versionName = p.versionName;
	        newInfo.versionCode = p.versionCode;
	        newInfo.icon = p.applicationInfo.loadIcon(content.getPackageManager());
	        res.add(newInfo);
	    }
	    return res; 
	}
}
